package com.bj.zspace.cloud.order.service.impl;

import com.bj.zspace.cloud.model.order.Order;
import com.bj.zspace.cloud.model.product.Product;
import com.bj.zspace.cloud.order.feign.ProductFeignClient;
import com.bj.zspace.cloud.order.service.OrderService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private DiscoveryClient discoveryClient;
    @Resource
    private RestTemplate restTemplate;
    @Resource
    private LoadBalancerClient loadBalancerClient;
    @Resource
    private ProductFeignClient productFeignClient;

    @Override
    public Order creteateOrder(Long productId, Long userId) {
        Product product = productFeignClient.getProduct(productId);
        Order order = new Order();
        order.setId(1L);
        order.setTotalAmount(product.getPrice().multiply(new BigDecimal(product.getNum())));
        order.setUserID(userId);
        order.setNickName("猪八戒");
        order.setAddress("高老庄");
        order.setProductList(Arrays.asList(product));
        return order;
    }
    /**
     * V3: RestTemplate负载均衡: RestTemplate + @LoadBalancer注解
     */
    private Product getProductFromRemoteLoadBalancerWithAnnotation(Long productId) {
        String url = "http://service-product/api/product/" + productId;
        ResponseEntity<Product> responseEntity = restTemplate.getForEntity(url, Product.class);
        log.info("请求地址:{}, response:{}", url, responseEntity);
        Product body = responseEntity.getBody();
        return body;
    }

    /**
     * V2: 负责均衡的LoadBalancerClient
     */
    private Product getProductFromRemoteLoadBalancer(Long productId) {
        ServiceInstance instances = loadBalancerClient.choose("service-product");
        String url = "http://" + instances.getHost() + ":" + instances.getPort() + "/api/product/" + productId;
        ResponseEntity<Product> responseEntity = restTemplate.getForEntity(url, Product.class);
        log.info("请求地址:{}, response:{}", url, responseEntity);
        Product body = responseEntity.getBody();
        return body;
    }

    /**
     * V1: 原始的DiscoveryClient
     */
    private Product getProductFromRemote(Long productId) {
        List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
        ServiceInstance serviceInstance = instances.get(0);
        String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/api/product/" + productId;
        ResponseEntity<Product> responseEntity = restTemplate.getForEntity(url, Product.class);
        log.info("请求地址:{}, response: {}", url, responseEntity.toString());
        Product body = responseEntity.getBody();
        return body;
    }



}
